import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';

class GameRecordPage extends StatefulWidget {
  const GameRecordPage({super.key});
  @override
  _GameRecordPageState createState() => _GameRecordPageState();
}

class _GameRecordPageState extends State<GameRecordPage> {
  int _correct = 0;
  int _wrong = 0;
  String _rate = "";

  @override
  void initState() {
    super.initState();
    _loadGameRecords();
  }

  Future<void> _loadGameRecords() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    int correct = prefs.getInt("correct") ?? 0;
    int wrong = prefs.getInt("wrong") ?? 0;
    double rate = (correct + wrong) == 0 ? 0 : (correct / (correct + wrong));
    NumberFormat formatter = NumberFormat("#0.0%", "en_US");
    String rateStr = formatter.format(rate);
    setState(() {
      _correct = correct;
      _wrong = wrong;
      _rate = rateStr;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('游戏记录'),
      ),
      body: Center(
          child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
        Text("回答正确：$_correct",
            style: const TextStyle(color: Colors.red, fontSize: 24)),
        const SizedBox(height: 20),
        Text("回答错误：$_wrong",
            style: const TextStyle(color: Colors.red, fontSize: 24)),
        const SizedBox(height: 20),
        Text("正确率：$_rate",
            style: const TextStyle(color: Colors.red, fontSize: 24)),
      ])),
    );
  }
}
